package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.javabean.OrderBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActity extends BaseActivity {

    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_order_detail_icon)
    ImageView icon;
    @BindView(R.id.activity_order_detail_name)
    TextView name;
    @BindView(R.id.activity_order_detail_time)
    TextView time;
    @BindView(R.id.activity_order_detail_money)
    TextView money;
    @BindView(R.id.activity_order_detail_textView)
    TextView textView;
    private OrderBean.DataBean.ListBean listBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);

        initData();

    }

    private void initData() {
        listBean = (OrderBean.DataBean.ListBean) getIntent().getSerializableExtra("text");
        String paymentWay = "";
        switch (listBean.getPaymentWay()) {
            case "02":
                paymentWay = "微信";
                break;
            case "03":
                paymentWay = "支付宝";
                break;
        }


        switch (listBean.getOrderType()) {
            case "1":
                title.setText("订单详情（押金）");
                textView.setText("订单编码：" + listBean.getOrderId() +
                        "\n支付方式：" + paymentWay +
                        "\n支付时间：" + listBean.getOrderPayTime());
                break;
            case "2":
                title.setText("订单详情（租车）");
                int Days = listBean.getOrderBuyDays().equals("") ? 0 : Integer.parseInt(listBean.getOrderBuyDays());
                String endTime = getOldDateByDay(parseServerTime(listBean.getOrderPayTime(), "yyyy-MM-dd HH:mm:ss"), Days, "yyyy.MM.dd");
                time.setText("租车日期：" + listBean.getOrderPayTime().substring(0, 4) + "." + listBean.getOrderPayTime().substring(5, 7) + "." +
                        listBean.getOrderPayTime().substring(8, 10) + "-" + endTime);
                textView.setText("订单编码：" + listBean.getOrderId() +
                        "\n支付方式：" + paymentWay +
                        "\n支付时间：" + listBean.getOrderPayTime() +
                        "\n新旧程度：" + listBean.getBicycleDepreciation() +
                        "%\n租车电话：" + listBean.getSellPhone() +
                        "\n租车地址：" + listBean.getBicycleExtractAddress());
                break;
            case "3":
                title.setText("订单详情（买车）");
                textView.setText("订单编码：" + listBean.getOrderId() +
                        "\n支付方式：" + paymentWay +
                        "\n支付时间：" + listBean.getOrderPayTime() +
                        "\n新旧程度：" + listBean.getBicycleDepreciation() +
                        "%\n提车电话：" + listBean.getSellPhone() +
                        "\n提车地址：" + listBean.getBicycleExtractAddress());
                break;
        }

        Glide.with(this).load(listBean.getBicyclePictureUrl()).into(icon);
        name.setText(listBean.getBicycleName());
        money.setText(listBean.getOrderMoney());

    }


    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }

    private Date parseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    private String getOldDateByDay(Date beginDate, int distanceDay, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat dft = new SimpleDateFormat(format);
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }
}
